#ifndef TAPP_H
#define TAPP_H

#include <Windows.h>
#include "twin.h"

BOOL TRegisterClass( LPCSTR class_name, UINT style = CS_DBLCLKS, HICON hIcon = 0, HCURSOR hCursor = 0,
					HBRUSH hbrBackground = 0, int classExtra = 0, int wndExtra = 0, LPCSTR menu_str = 0 );
BOOL TRegisterClassU8( LPCSTR class_name, UINT style = CS_DBLCLKS, HICON hIcon = 0, HCURSOR hCursor = 0,
					  HBRUSH hbrBackground = 0, int classExtra = 0, int wndExtra = 0, LPCSTR menu_str = 0);
BOOL TRegisterClassV( const void *class_name, UINT style = CS_DBLCLKS, HICON hIcon = 0,
					 HCURSOR hCursor = 0, HBRUSH hbrBackground = 0, int classExtra = 0, int wndExtra = 0,
					 const void *menu_str = 0 );

class TApp 
{
public:
	TApp( HINSTANCE hInstance, LPSTR cmdLine, int cmdShow );
	virtual ~TApp();

	virtual void	InitWindow() = 0;
	virtual int		Run();
	virtual BOOL	PreProcMsg( MSG* msg );

	LPCSTR	GetDefaultClass() { return _defaultClass; }
	LPCVOID	GetDefaultClassV() { return (void *)_defaultClassV; }
	void	AddWin(TWin *win) { _preWnd = win; }
	void	AddWinByWnd( TWin *win, HWND hWnd ) 
	{
		win->_hWnd = hWnd; _hash->Register( win, _hash->MakeHashId(hWnd) );
	}
	void	DelWin( TWin* win ) { _hash->UnRegister(win); }

	static TApp *GetApp() { return tapp; }
	static HINSTANCE GetInstance() { return tapp->_hInstance; }
	static LRESULT CALLBACK WinProc(HWND hW, UINT uMsg, WPARAM wParam, LPARAM lParam);

protected:
	TWin* SearchWnd(HWND hWnd) { return (TWin *)_hash->Search(&hWnd, _hash->MakeHashId(hWnd)); }
	virtual BOOL InitApp(void);

protected:
	static TApp*	tapp;
	TWinHashTbl*	_hash;
	LPCSTR			_defaultClass;
	LPCVOID			_defaultClassV;

	LPSTR			_cmdLine;
	int				_cmdShow;
	TWin*			_mainWnd;
	TWin*			_preWnd;
	HINSTANCE		_hInstance;

};

#endif